package com.chenglai.seckill.service.impl;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.seckill.dao.SeckillInfoMapper;
import com.chenglai.seckill.pojo.SeckillInfo;
import com.chenglai.seckill.service.SeckillInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/23 14:32
 */
@Service
public class SeckillInfoServiceImpl implements SeckillInfoService {

    @Resource
    private SeckillInfoMapper seckillInfoMapper;

    @Override
    public PageInfo getSeckillAllList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(SeckillInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("seckillId").desc();
        List<SeckillInfo> seckillInfos = seckillInfoMapper.selectByExample(example);
        return new PageInfo(seckillInfos);
    }

    @Override
    public SeckillInfo getLatelySeckillInfo(Integer shopId) {
        SeckillInfo seckillInfo = seckillInfoMapper.getLatelySeckillInfo(shopId, DateFormatHelper.currentTimes());
        return seckillInfo;
    }

    @Override
    public void editSeckillInfo(SeckillInfo seckillInfo) {
        if (seckillInfo.getSeckillId() == null) {
            //添加
            seckillInfoMapper.insertSelective(seckillInfo);
        } else {
            //修改
            seckillInfoMapper.updateByPrimaryKeySelective(seckillInfo);
        }
    }

    @Override
    public SeckillInfo detail(Integer seckillId) {
        return seckillInfoMapper.selectByPrimaryKey(seckillId);
    }


}
